let num=1;
Page({
    data: {
        percent: 0,
        status: 'normal',
        page:1,
        total:0,
        questList:[],
        timer:0,
        tlist:[],
        okerrStatu:[],
        coll:[],
        collstatu:[],
        answer:[],
        answerAll:[],
        items:null,
        idss:'',
        okNum:0,
        scoreDan:0,
        btn:'下一题',
        show:false,
        showTiTable:false,
        check:false,
        analysis:false,
        starStatu:false,
        sele:'',seleA:false,seleB:false,seleC:false,seleD:false,
        errA:false,errB:false,errC:false, errD:false,
        min:'',sec:'',
        hasBeanDo:{okorno:''},
        backforquestion:[{
            seleA:false,seleB:false,seleC:false,seleD:false,
            errA:false,errB:false,errC:false, errD:false,analysis:false
        }]
    },
    onLoad(option){
        num=1
        let id =option.id
        this.data.idss = id
       
        this.showTlist(id)
        this.timeGo();
        
        //题库
        wx.cloud.database().collection('questbank')
        .doc(id)
        .get()
        .then(res =>{
            this.setData({
                kname:res.data.kname            
            })
        })
        
        // wx.setStorageSync('coll', null)
    },

    /**
     *   方法区---------
     * 
    **/

    whetherDoTi(){
       
    },
    //下一题
    next(e){
        //1.判断是否做过
        if(this.data.hasBeanDo[num]!=undefined){
            // console.log("9090909",this.data.hasBeanDo[num])
            num++
            this.setData({
                page:num,
                
            })
            this.whetherColl()
            this.statu()
           
            return

        }
        // console.log("9090908",this.data.hasBeanDo[num])
        //2.做题后，判空
        if(this.data.sele ==''){

            wx.showToast({
                icon:'none',
                title: '请选择选项',
            })
            return;
        }

        //3、清空上题状态
            this.setData({
             errA:false, errB:false, errC:false,errD:false,
             seleA:false,seleB:false,seleC:false,seleD:false,
             check:false,
             analysis:false
         })
         
         
         //5、判断不超总数
         if(num < this.data.total){
             num++;
             this.setData({
                 page:num
                 
                })
                //4、判断收藏
            this.whetherColl()
  
        }else if(num == this.data.total){
            clearInterval(this.data.timer)
            wx.redirectTo({
                url: '/pages/results/results?total='+this.data.total+'&oknum='+this.data.okNum+'&min='+this.data.min+'&sec='+ this.data.sec+'&score='+this.data.scoreDan
              })
            
 
        } 
        this.showTlist(this.data.idss)
        this.data.sele =''
    },

    //选项
    select(e){
        //判读是否作答,若以作答点击无效
        if(this.data.check){
            return
        }
 
        this.setData({
            sele : e.currentTarget.dataset.sel,
            show :true
        })

        if(num == this.data.total){
            this.setData({
                btn:'提交答案'
            })
            
        }

        //单选与多选判断
        this.radioAndJudge()
       
       this.setData({
           items : this.data.okerrStatu,
           check:true
        })
        this.data.hasBeanDo[num-1]={okorno:true}

    },
    //后台违反开闭原则，所以需要相应冗余代码来处理
    radioAndJudge(){
        if(this.data.sele == this.data.questList[num-1].A){
            //正确
            if(this.data.questList[num-1].answer.includes(this.data.sele)){
                this.setData({
                   seleA:true,
               })

               this.data.okNum++
               this.data.scoreDan=this.data.scoreDan+2
               this.data.okerrStatu.push({okerr:true})
               this.data.backforquestion[num-1] = {
                   seleA:true,seleB:false,seleC:false,seleD:false,
                   errA:false,errB:false,errC:false, errD:false,analysis:false}
                   // console.log(this.data.backforquestion)
                   console.log(this.data.okerrStatu)
            }else{
              //错误
               this.setData({
                     errA:true, 
                     analysis:true
               })
               this.addErrTi('A')
               this.data.okerrStatu.push({okerr:false}) 
               this.data.backforquestion[num-1] = {
                   seleA:false,seleB:false,seleC:false,seleD:false,
                   errA:true,errB:false,errC:false, errD:false,analysis:true}
           }
       //B选项内容区
       }else if(this.data.sele == this.data.questList[num-1].B){
           if(this.data.questList[num-1].answer.includes(this.data.sele)){
               this.setData({
                  seleB:true,
              })
              this.data.okNum++
              this.data.scoreDan=this.data.scoreDan+2
              this.data.okerrStatu.push({okerr:true})
              this.data.backforquestion[num-1] = {
               seleA:false,seleB:true,seleC:false,seleD:false,
               errA:false,errB:false,errC:false, errD:false,analysis:false}
              
           }else{
               this.setData({
                   errB:true, 
                   analysis:true
               })
               this.addErrTi('B')
               this.data.okerrStatu.push({okerr:false}) 

               this.data.backforquestion[num-1] = {
                   seleA:false,seleB:false,seleC:false,seleD:false,
                   errA:false,errB:true,errC:false, errD:false,analysis:true}
           }
       //C选项内容区
       }else if(this.data.sele == this.data.questList[num-1].C){
           if(this.data.questList[num-1].answer.includes(this.data.sele)){
               this.setData({
                  seleC:true,
              })
              this.data.okNum++
              this.data.scoreDan=this.data.scoreDan+2
              this.data.okerrStatu.push({okerr:true})
              this.data.backforquestion[num-1] = {
               seleA:false,seleB:false,seleC:true,seleD:false,
               errA:false,errB:false,errC:false, errD:false,analysis:false}
           }else{
               this.setData({
                   errC:true, 
                   analysis:true
               })
               this.addErrTi('C')
               this.data.okerrStatu.push({okerr:false}) 

               this.data.backforquestion[num-1] = {
                   seleA:false,seleB:false,seleC:false,seleD:false,
                   errA:false,errB:false,errC:true, errD:false,analysis:true}
           }
       //D选项内容区
       }else if(this.data.sele == this.data.questList[num-1].D){

           if(this.data.questList[num-1].answer.includes(this.data.sele)){
               this.setData({
                  seleD:true,
              })
               this.data.okNum++
               this.data.scoreDan=this.data.scoreDan+2
               this.data.okerrStatu.push({okerr:true})
              this.data.backforquestion[num-1] = {
               seleA:false,seleB:false,seleC:false,seleD:true,
               errA:false,errB:false,errC:false, errD:false,analysis:false}
           }else{
               this.setData({
                   errD:true, 
                   analysis:true,
               })
               this.addErrTi('D')
               this.data.okerrStatu.push({okerr:false}) 
               this.data.backforquestion[num-1] = {
                   seleA:false,seleB:false,seleC:false,seleD:false,
                   errA:false,errB:false,errC:false, errD:true,analysis:true}
           }
       }
    },

    //接收查询题集
    showTlist(id){      
        wx.cloud.database().collection('question')
        .where({
          kname:id
        })
        .get()
        .then(res => {
            console.log("success1",res)
            console.log(id)
            this.setData({
                   questList:res.data,
                   total:res.data.length
                })
               let answer =res.data[num-1].answer

               this.data.answer[num-1]={selected:answer.toString().substring(0,1)}
                 this.setData({
                      answerAll:this.data.answer
                 })
        }).catch(err => {
            console.log("err",err)
        })    
    
        console.log(this.data.answerAll)
    },

    //答题卡跳转  
      goQuest(e){
       //判断是否做过
       let n = e.currentTarget.dataset.nums
       if(this.data.hasBeanDo[n-1]==undefined){
            return
        }
 
        num = e.currentTarget.dataset.nums
        this.setData({
            page:num,
            
        })
    
        if(num < this.data.total){
            this.setData({
                btn:'下一题'
            })
            
        }
        this.statu()      

        //判断收藏状态
        this.whetherColl()

        this.onClose()
        
      },

      //状态赋值
    statu(){
        let {errA,errB,errC,errD,seleA,seleB,seleC,seleD,analysis}=this.data.backforquestion[num-1]
        this.setData({
           seleA,seleB,seleC,seleD,errA,errB,errC,errD,analysis
        })
      },

      //计时开始
      timeGo(){
        var timeIndex = 0;
        var  that=this
     
         var  timers = setInterval( () => {
            var hour = parseInt(timeIndex / 3600);
            var minutes = parseInt((timeIndex % 3600) / 60);
            var seconds = parseInt(timeIndex % 60);
            hour = hour < 10 ? "0" + hour : hour;
            minutes = minutes < 10 ? "0" + minutes : minutes;
            seconds = seconds < 10 ? "0" + seconds : seconds;
            this.setData({
                min : minutes,
                sec : seconds

             })

            timeIndex++;
        }, 1000)
        this.setData({timer:timers}) 

    },
    //分数计算
    score(opition){
        scoreSingle
    },
    //上拉框
     showPopup() {
        if(this.data.sele ==''){

            wx.showToast({
                icon:'none',
                title: '做完本题,可点击答题卡',
            })
            return;
        }
        this.setData({ showTiTable: true });
      },
      onClose() {
        this.setData({ showTiTable: false });
      },  
      //添加错题本
     async addErrTi(select){
        let db =  wx.cloud.database().collection('wrong')
        let user = wx.getStorageSync('userInfo') 

        if(user==null){return}
       
        //查询此用户的错题表
        let find = await db.where({
            _openid:user._openid
        }).get()
        console.log(find)

        //判断是否错了相同的题
        // let tablerrTi = find.data[num-1].questionID
        let questionall= this.data.questList[num-1]
        let questionID= this.data.questList[num-1]._id
        // console.log("033",questionID)

        for(let i=0;i<find.data.length;i++){

            if(find.data[i].questionID==questionID){
                // console.log(find.data[i].questionID==questionID)
                return
            }
           
        }
 

        //添加操作
         await db.add({
            data:{
                questionID,
                myselect:select,
                questionall
            }
        })
     
    },
      //添加收藏
      star(){
        //判断是否登录
        let user = wx.getStorageSync('userInfo') 
        if(user==null){
            wx.showToast({
                icon:'none',
              title: '登录后方可收藏',
            })
            return
        }
        
        //收藏操作
        wx.setStorageSync('coll', null)

             this.setData({
                starStatu: !this.data.starStatu,
            })
            if(this.data.starStatu){
                this.data.coll[num-1] = this.data.questList[num-1]
                this.data.collstatu[num-1] = true
                wx.showToast({
                    icon:'none',
                    title: '已收藏'
                })
            }else{
                this.data.coll[num-1] = null
                this.data.collstatu[num-1] = false
                wx.showToast({
                    icon:'none',
                    title: '已取消收藏'
                })
            }

            wx.setStorageSync('coll',  this.data.coll)
      },
      //判断是否收藏
      whetherColl(){
        if(this.data.collstatu[num-1]==undefined){
            this.setData({
                starStatu:false
            })
            // console.log("6666")
         }else if(this.data.collstatu[num-1]){
            this.setData({
                starStatu:true
            })
         }else{
            this.setData({
                starStatu:false
            })
         }  
      }

});